package com.juzi.wopu.web;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.juzi.wopu.domain.SysCode;
import com.juzi.wopu.domain.SysRole;
import com.juzi.wopu.domain.SysUser;
import com.juzi.wopu.domain.SysUserRole;
import com.juzi.wopu.domain.SysUserRoleResponse;
import com.juzi.wopu.query.PageList;
import com.juzi.wopu.query.SysUserRoleQuery;
import com.juzi.wopu.service.ISysCodeService;
import com.juzi.wopu.service.ISysRoleService;
import com.juzi.wopu.service.ISysUserRoleService;
import com.juzi.wopu.service.ISysUserService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.interceptor.annotations.InputConfig;

public class SysUserRoleAction extends CRUDAaction<SysUserRole> {
	
	private static final long serialVersionUID = 1L;
	
	private ISysUserRoleService sysUserRoleService;
	private ISysUserService sysUserService;
	private ISysRoleService sysRoleService;
	private ISysCodeService sysCodeService;
	private SysUserRole sysUserRole = new SysUserRole();
	
	private String mobile;
	
	private SysUserRoleQuery baseQuery = new SysUserRoleQuery();
	
	private PageList pageList;
	
	SysUserRoleResponse sysUserRoleResponse=new SysUserRoleResponse();
	
	private List<SysUserRoleResponse> data=new ArrayList<>();
	
	public String getMobile() {
		return mobile;
	}

	public void setMobile(String mobile) {
		this.mobile = mobile;
	}

	public void setSysUserRoleService(ISysUserRoleService sysUserRoleService) {
		this.sysUserRoleService = sysUserRoleService;
	}
	
	public void setSysRoleService(ISysRoleService sysRoleService) {
		this.sysRoleService = sysRoleService;
	}
	
	public void setSysCodeService(ISysCodeService sysCodeService) {
		this.sysCodeService = sysCodeService;
	}
	
	@Override
	protected void list() {
		/**
		 * 如果局点不为空，拿到登录进去的局点或者切换的局点
		 * 如果为空就拿到然后赋值给局点这个变量
		 */
		if(companyName!=null){
			ActionContext.getContext().getSession().put("companyName", companyName);
		}
		else {
			companyName=(String) ActionContext.getContext().getSession().get("companyName");
		}
		Map<Long, SysUser> idSysUserMap=new HashMap<>();
		sysUserService.getSysUserMap(idSysUserMap, null);
		Map<Long, SysRole> sysRoleMap=new HashMap<>();
		sysRoleService.getSysRoleMap(sysRoleMap, null);
		
		Map<Long, SysCode> sysCodeMap=new HashMap<>();
		Map<String, SysCode> map=new HashMap<>();
		sysCodeService.getSysCodeMap(sysCodeMap, map);
		
		Long iCompany= map.get(companyName).getiId();
		baseQuery.setiCompany(iCompany);
		this.pageList = sysUserService.findByQuery(baseQuery);
		
		
		this.pageList = sysUserRoleService.findByQuery(baseQuery);
		if(pageList!=null && pageList.getRows()!=null && pageList.getRows().size()>0){
			for(int i=0;i<pageList.getRows().size();i++){
				SysUserRole sysUserRole=(SysUserRole) pageList.getRows().get(i);
				SysUserRoleResponse response=new SysUserRoleResponse(sysUserRole);
				SysUser user=idSysUserMap.get(sysUserRole.getiUserId());
				if(user==null){
					response.setUserName("");
				}else {
					response.setUserName(user.getsUserNo());
				}
				
				response.setRoleName(sysRoleMap.get(sysUserRole.getiRole()).getsName());
				response.setCompName(sysCodeMap.get(sysUserRole.getiCompany()).getsName());
				data.add(response);	
			}
		}
	}

	@Override
	public String input() throws Exception {
		List<SysRole> list=sysRoleService.getAll();
		putContext("list", list);
		return INPUT;
	}

	public void validateSave() {

	}

	@Override
    @InputConfig(methodName="input")
	public String save() throws Exception {
		Map<String, SysCode> map=new HashMap<>();
		Map<Long, SysCode> map1=new HashMap<>();
		sysCodeService.getSysCodeMap(map1, map);
		
		companyName=(String) ActionContext.getContext().getSession().get("companyName");
		long companyId=map.get(companyName).getiId();
		if(id==null){
			baseQuery.setCurrentPage(Integer.MAX_VALUE);
			baseQuery.setCurrentPage(Integer.MAX_VALUE);
			SysUser sysUser=new SysUser();
			sysUser.setsUserNo(sysUserRoleResponse.getUserName());
			sysUser.setsMobile(sysUserRoleResponse.getMobile());
			sysUser.setDtInsert(new Date());
			sysUser.setiType(0L);
			sysUser.setiStatus(0L);
			sysUser.setsPass("123AbCdE");
			sysUser.setsSex("保密");
			sysUser.setiCompany(companyId);
		//	sysUserService.save(sysUser);
			
			sysUserRole.setDtInsert(new Date());
			sysUserRole.setiCreateUser(1L);
//			companyName=(String) ActionContext.getContext().getSession().get("companyName");
//			Map<String, SysCode> nameCodeMap=new HashMap<>();
//			sysCodeService.getSysCodeMap(null, nameCodeMap);//得到sName对应SysCode的map
//			long iCompany=nameCodeMap.get(companyName).getiId();
//			sysUserRole.setiCompany(iCompany);
			sysUserRole.setICompany(1901L);
			sysUserRoleService.save(sysUserRole);
		}else {
			sysUserRoleService.update(sysUserRole);
		}
		return RELOAD;
	}

	@Override
	public String delete() throws Exception {
		HttpServletResponse response=ServletActionContext.getResponse();
		response.setContentType("text/json;charset=UTF-8");
		PrintWriter out = response.getWriter();
		try {
			if (id != null) {
				sysUserRoleService.delete(id);
				out.print("{\"success\":true,\"msg\":\"删除成功\"}");
			}else{
				out.print("{\"success\":false,\"msg\":\"没有删除成功\"}");
			}
		} catch (Exception e) {
			e.printStackTrace();
			out.print("{\"success\":false,\"msg\":\"异常信息:"+e.getMessage()+"\"}");
		}
		return null;
	}

	// input前置方法
	@Override
	public void prepareInput() throws Exception {
		if (id != null) {// 最主要做回显
			sysUserRole = sysUserRoleService.get(id);// 变成栈顶对象
		}
	}

	@Override
	public void prepareSave() throws Exception {
		if (id == null) {
			sysUserRole = new SysUserRole();
		} else {
			sysUserRole = sysUserRoleService.get(id);
		}
	}

	// 圧栈后院获取
	@Override
	public SysUserRole getModel() {
		return sysUserRole;
	}

	public PageList getPageList() {
		return pageList;
	}

	public SysUserRoleQuery getBaseQuery() {
		return baseQuery;
	}

	public void setBaseQuery(SysUserRoleQuery baseQuery) {
		this.baseQuery = baseQuery;
	}

	public ISysUserService getSysUserService() {
		return sysUserService;
	}

	public void setSysUserService(ISysUserService sysUserService) {
		this.sysUserService = sysUserService;
	}

	public ISysUserRoleService getSysUserRoleService() {
		return sysUserRoleService;
	}

	public List<SysUserRoleResponse> getData() {
		return data;
	}

	public void setData(List<SysUserRoleResponse> data) {
		this.data = data;
	}
}